home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-04-22 | 842 b | 40 lines | [TEXT/CWIE] |
- /* -------------------------------------------------------------
- This applet finds the primes between 1 and 100.
-
- Java's classes: Applet (applet)
- System (lang)
-
- Custom classes: IsOdd
-
- ------------------------------------------------------------- */
-
- public class NextPrime3 extends java.applet.Applet {
- public void init() {
-
- int primeIndex, candidate, i, last;
- boolean isPrime;
-
- System.out.println( "Prime #1 is 2." );
-
- candidate = 3;
- primeIndex = 2;
-
- while ( primeIndex <= 100 ) {
-
- isPrime = true;
- last = (int)Math.sqrt( candidate );
-
- for ( i = 3; (i <= last) && isPrime; i += 2 ) {
- if ( (candidate % i) == 0 )
- isPrime = false;
- }
-
- if ( isPrime ) {
- System.out.println( "Prime " + primeIndex + " is " + candidate );
- primeIndex++;
- }
-
- candidate += 2;
- }
- }
- }